<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_INS_SHOP_SHOP.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_INS_SHOP_SHOP,$login['staff_auth']);

$matrix_cd = "";
if ( isset( $_GET['w'] )  ) {
	$matrix_cd =  $_GET['w'];
}
if ( empty( $matrix_cd ) ) {
	echo "パラメータエラー";
	exit;
}

$wData    = Class_M_OTHER::get_by_matrix_cd( $matrix_cd );
$dispweek = $wData["year4"].'年'.$wData["week_cd"].'週';
if ( $wData["[week_seq"] ) {
	$dispweek = $dispweek."回";
}
$dispweek = $dispweek."(". Class_PWD::int8_to_date($wData["flier_date"]) .")";



//$week_data = Class_M_OTHER::get_by_matrix_cd( $matrix_cd );

$dao = new Class_M_INS_SHOP_SHOP ;
$dao -> setTableNameWeek($matrix_cd);

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';
	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$datas  = $dao->getByShopCd( $_GET['id'] );
		//データ整形
		$forms["id"] = $_GET['id'];
		$patterns = Class_M_OTHER::get_pattern_array();
		
		$firstelement = TRUE;
		foreach ($datas as $k=>$v) {
			if ($firstelement) {
				$firstelement = FALSE;
				$forms["shop_name"]   = $v["shop_name"];
				$forms["shop_cd"]     = $v["shop_cd"];
				$forms["disp_ng"]     = $v["disp_ng"];
				$forms["m_user_name"] = $v["m_user_name"];
				$forms["u_time"]      = $v["u_time"];
			}
			$forms["mutirow"] []=array("ins_shop_cd" => $v["ins_shop_cd"], 
								"ins_shop_name"      => $v["ins_shop_name"] ,
								"insert_conums"      => $v["insert_conums"],
								"area_pattern_cd"    => $v["area_pattern_cd"],
								"area_pattern_name"  => $patterns[$v["area_pattern_cd"] ],
								"area_adnums"        => $v["area_adnums"],
								"area_memo"          => $v["area_memo"],
								"area_disp_ng"       => $v["area_disp_ng"] == 1 ? 1:0 ,
								"memo"               => $v["memo"]);
		}
		//$forms["stop_date"] = Class_PWD::int8_to_date($forms["stop_date"]) ;
		
	}else{
		//$forms['auth'] = 0;
	}
	//print_r($forms["mutirow"]);
}else{
	$forms = $_POST;
	
	if (isset($forms["ins_shop_cd"])) {
		foreach ($forms["ins_shop_cd"] as $k=>$v) {
			$forms["mutirow"] []=array("ins_shop_cd"=> $v, 
			"ins_shop_name"   => $forms["ins_shop_name"][$k],
			"insert_conums"   => empty($forms["insert_conums"][$k]) ? 0 : $forms["insert_conums"][$k],
			"area_pattern_cd" => empty($forms["area_pattern_cd"][$k]) ? 0 : $forms["area_pattern_cd"][$k],
			"area_adnums"     => empty($forms["area_adnums"][$k]) ? 0 : $forms["area_adnums"][$k],
			"area_memo"       => $forms["area_memo"][$k],
			"area_disp_ng"    => $forms["area_disp_ng"][$k],
			"memo"            => $forms["memo"][$k]
			);
		}
	} 

	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		
		//foreach ($chk as $kcol => $chkval) {
			//$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		//}
		foreach ($forms["mutirow"]as $k=>$v) {
			$err_mes1 = $err->check( $v["insert_conums"],  array( "NUM" ) );
			if  ( !empty($err_mes1) ) {
				$err_mes['insert_conums'] = "<p class='error'>折込会社部数は半角数字で入力して下さい</p>";
			}
			
			$err_mes2 = $err->check( $v["area_adnums"],  array( "NUM" ) );
			if  ( !empty($err_mes2) ) {
				$err_mes['area_adnums'] = "<p class='error'>地域広告部数は半角数字で入力して下さい</p>";
			}
		}
		//if( $forms['id'] == '' ) $err_mes['login_pwd']	= $err->check( $forms['login_pwd'], $chk['login_pwd'] );
		if( $err_mes['shop_cd'] == '' ){
			if( $forms['id'] == '' ){
				$where = sprintf(" shop_cd = '%s'", $forms['shop_cd'] );
				if ( $dao->exits($where) ) {
					$err->clear = false;
					$err_mes['shop_cd'] = '<p class="error">この店舗は重複しています</p>';
				}
			}
		}
		
		//if ( $err->clear ){
			//$cols     = $dao->get_cols();
			
			//foreach($cols as $kcol => $val) {
			//	$values[$kcol] = $forms[$kcol] ;
			//}
			//桁数チェック
			//$maxlens = $dao->get_maxlens();
			//foreach ( $maxlens as $k => $v ) {
			//	$err_mes[$k] =$err->check_size($values[$k], $v);
			//}
		//}
	
		if( $err->clear ){
			try {
				$dao->begin_trans();
				$executeflg = TRUE;
				
				if( $forms['id'] == '' ){
					foreach ($forms["mutirow"] as $k=>$v) {
						$values = array();
						$values["shop_cd"]         =  $forms["shop_cd"]; 
						$values["ins_shop_cd"]     =  $v["ins_shop_cd"];
						//$values["ins_shop_name"]   =  $v["ins_shop_name"];
						$values["insert_conums"]   =  $v["insert_conums"];
						$values["area_pattern_cd"] = $v["area_pattern_cd"];
						$values["area_adnums"]     =  $v["area_adnums"];
						$values["area_memo"]       =  $v["area_memo"];
						$values["area_disp_ng"]    =  $v["area_disp_ng"];
						$values["memo"]    		   =  $v["memo"];
						
						$values["ins_shop_shop_cd"] =  $values["shop_cd"]."_".$values["ins_shop_cd"];
						$values["disp_ng"]          =  0;
	
						
						if ($dao->add0($values)) {
						} else {
							$executeflg = FALSE;
						}
					}
					if  ($executeflg) {
						$dao->commit_trans();
						$smarty->assign('message', '登録が完了しました。');
					} else {
						$dao->rollback_trans();
						$smarty->assign('message', '登録が失敗しました。');
					}
				}else{
					/*更新か削除かチェック整形*/
					$dataadd  = array();
					$datadel  = array();
					$dataedit = array();
					
					$disp_ng = $forms["disp_ng"]?$forms["disp_ng"]:0;
					
					$dbarray = $dao->getByShopCd( $forms["shop_cd"] );
					//削除.更新のデータ作成
					foreach($dbarray as $k=>$v) {
						$delflg = TRUE;
						foreach($forms["mutirow"] as $kk=>$vv) {
							if ($v["ins_shop_cd"] == $vv["ins_shop_cd"] ) {
								$dataedit[] = array("ins_shop_cd"      => $vv["ins_shop_cd"],
										    "ins_shop_shop_cd" => $forms["shop_cd"]."_".$vv["ins_shop_cd"],  
											//"ins_shop_name"   => $vv["ins_shop_name"],
											"insert_conums"   => $vv["insert_conums"],
											"area_pattern_cd" =>$vv["area_pattern_cd"],
											"area_adnums"     => $vv["area_adnums"],
											"area_memo"       => $vv["area_memo"],
											"area_disp_ng"    => $vv["area_disp_ng"],
											"memo"            => $vv["memo"]
											);
								$delflg = FALSE;
								break;
							}
						}
						if ($delflg){
							$datadel[]  = $v["ins_shop_shop_cd"];
						}
					}
					
					//追加のデータ作成
					foreach($forms["mutirow"] as $k=>$v) {
						$taddflg = TRUE;
						foreach($dbarray as $kk=>$vv) {
							if ($v["ins_shop_cd"] == $vv["ins_shop_cd"] ) {
								$taddflg = FALSE;
								break;
							}
						}
						if ($taddflg){
							$dataadd[] = array("ins_shop_cd"      => $v["ins_shop_cd"],
												"ins_shop_name"   => $v["ins_shop_name"],
												"insert_conums"   => $v["insert_conums"],
												"area_pattern_cd" => $v["area_pattern_cd"],
												"area_adnums"     => $v["area_adnums"],
												"area_memo"       => $v["area_memo"],
												"area_disp_ng"    => $v["area_disp_ng"],
												"memo"            => $v["memo"]
												);
						}
					}
						
					
					foreach ($datadel as $k=>$v) {
						if ($dao->remove($v)) {
						} else {
							$executeflg = FALSE;
						}
					}
					foreach ($dataedit as $k=>$v) {
						$values = array();
						$values["shop_cd"]         =  $forms["shop_cd"]; 
						$values["ins_shop_cd"]     =  $v["ins_shop_cd"];
						//$values["ins_shop_name"]   =  $v["ins_shop_name"];
						$values["insert_conums"]   =  $v["insert_conums"];
						$values["area_pattern_cd"] =  $v["area_pattern_cd"];
						$values["area_adnums"]     =  $v["area_adnums"];
						$values["area_memo"]       =  $v["area_memo"];
						$values["area_disp_ng"]    =  $v["area_disp_ng"];
						$values["memo"]    		   =  $v["memo"];
						$values["disp_ng"]          =  $disp_ng;
						
						if ($dao->edit( $v["ins_shop_shop_cd"] ,$values)) {
						} else {
							$executeflg = FALSE;
						}
					}
					foreach ($dataadd as $k=>$v) {
						$values = array();
						$values["shop_cd"]         =  $forms["shop_cd"]; 
						$values["ins_shop_cd"]     =  $v["ins_shop_cd"];
						//$values["ins_shop_name"]   =  $v["ins_shop_name"];
						$values["insert_conums"]   =  $v["insert_conums"];
						$values["area_pattern_cd"] =  $v["area_pattern_cd"];
						$values["area_adnums"]     =  $v["area_adnums"];
						$values["area_memo"]       =  $v["area_memo"];
						$values["area_disp_ng"]    =  $v["area_disp_ng"];
						$values["memo"]    		   =  $v["memo"];
						$values["ins_shop_shop_cd"] =  $values["shop_cd"]."_".$values["ins_shop_cd"];
						$values["disp_ng"]          =  $disp_ng;
						
						if ($dao->add0($values)) {
						} else {
							$executeflg = FALSE;
						}
					}
					if  ($executeflg) {
						$dao->commit_trans();
						$smarty->assign('message', '更新が完了しました。');
					} else {
						$dao->rollback_trans();
						$smarty->assign('message', '更新が失敗しました。');
					}
				}
			
			} catch (Exception $e) {
				$dao->rollback_trans();
				$smarty->assign('message', 'ＤＢエラーで失敗しました。');
			}
			
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}

$forms["w"]      = empty( $forms["w"]  )? $matrix_cd: $forms["w"] ;
$forms["dispw"]      = empty( $forms["dispw"]  )? $dispweek : $forms["dispw"] ;

if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);
$smarty->assign('areaflags', $aoki_area_user_flags );

/*
$isG4  = FALSE;
if   ($login['staff_auth'] == 'G-4') {
	$isG4 = TRUE;
}
$smarty->assign('$isG4', $isG4 );

if ($phase != 'complete') {
	$block = Class_M_OTHER::get_block_array(PLEASE_SELECT_ARR);
	$smarty->assign('block', $block );
	
	$local= Class_M_OTHER::get_local_array(PLEASE_SELECT_ARR);
	$smarty->assign('local', $local );
}
		

*/
$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('minsshopshop_form.html');
?>